package com.hp.attendance.serlvet.homepage;

import com.hp.attendance.entity.Attendance;
import com.hp.attendance.service.HomepageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;

@WebServlet("/signIn")
public class SignInServlet extends HttpServlet {
    private HomepageService homepageService = new HomepageService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //上班签到功能
        try {
            //获取当前时间的年月日
            LocalDate localDate = LocalDate.now();
            Date day = new SimpleDateFormat("yyyy-MM-dd").parse(String.valueOf(localDate));
            //获取当前时间时分秒
            LocalTime localTime = LocalTime.now();
            Date start_time = new SimpleDateFormat("HH:mm:ss").parse(String.valueOf(localTime));
            //获取员工编号
            int employee_number = Integer.parseInt(req.getParameter("employee_number"));
            //创建一个Attendance
            Attendance attendance = new Attendance();
            attendance.setDay(day);
            attendance.setStart_time(start_time);
            attendance.setEmployee_number(employee_number);
            //获取时间的时分秒
            int hour = localTime.getHour();
            int minute = localTime.getMinute();
            int second = localTime.getSecond();
            //判断开始打卡的签到类型
            if (hour >= 9 && minute > 0 && second > 0) {
                attendance.setStart_type("迟到");
            }
            if ((hour>7 && minute>0 && second>0) && hour<9) {
                attendance.setStart_type("正常");
            }

            //判断打卡时间段
            if (hour<=12) {
                attendance.setTime_type("上午");
            } else {
                attendance.setTime_type("下午");
            }

            //每个员工只能打卡一次
            //查询指定员工当天的考勤信息
            Attendance att = homepageService.getAttendance(day, employee_number);
            //当员工无当天考勤的时候  可以打卡
            if (att.getDay() == null) {
                homepageService.addClockIn(attendance);
            }
            //跳转到主页
            req.getRequestDispatcher("/homepage").forward(req, resp);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
